﻿using System.Linq;
using Common;
using Common.EventHandler;
using Domain.Common;
using Domain.Officers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UIComponentSprite.Building.Talents;

namespace UIComponentSprite.Building.Menu
{
    /// <summary>
    /// 菜单项
    /// </summary>
    public class MenuItemSprite : UserControlledSprite
    {
        public ImageTextSprite TextSprite;
        public MenuItemStatus Status = MenuItemStatus.Normal;
        public MenuSprite ChildMenuSprite;//单击时子菜单
        public MenuSprite MenuSprite;//所属菜单
        public string Text;
        public MenuItemSprite(string text)
        {
            this.MoveOnEvent += new MoveOnEventHandler(this.MoveOnEventHandler);
            this.MoveOutEvent += new MoveOutEventHandler(this.MoveOutEventHandler);
            this.ClickLeftEvent += new ClickLeftEventHandler(this.ClickLeftEventHandler);
            this.Text = text;
            this.Showing = true;
            DrawPosition.Width = 100;
            DrawPosition.Height = 25;
            TextSprite = new ImageTextSprite(text, new Rectangle(0, 0, 50, 23));

        }
        public override void Draw(GameTime gameTime)
        {
            if (!Showing)
                return;
            SpriteBatch.Draw(this.Image, this.DrawPosition, Color.White);
            TextSprite.Draw(gameTime);
        }

        public void Update(GameTime gameTime, Point position)
        {
            if (!Showing)
                return;
            DrawPosition.X = position.X + ((Status == MenuItemStatus.Select && this.ChildMenuSprite != null) ? 10 : 0);//菜单项被选中且有子菜单才靠右移动一点距离
            DrawPosition.Y = position.Y;
            TextSprite.DrawPosition.X = DrawPosition.X + DrawPosition.Width * 1 / 4;
            TextSprite.DrawPosition.Y = DrawPosition.Y + DrawPosition.Height * 1 / 6;
            TextSprite.Update(gameTime);
            MoveOnEventListener("1," + (DrawPosition.X + DrawPosition.Width) + "|" + DrawPosition.Y);
            MoveOutEventListener(false);
            ClickLeftEventListener(true);
            LoadPic();
        }
        /// <summary>
        /// 鼠标移入事件处理器
        /// </summary>
        /// <param name="args"></param>
        public void MoveOnEventHandler(object args)
        {
            this.Image = null;
            this.Status = MenuItemStatus.Select;

        }
        /// <summary>
        /// 鼠标移出事件处理器
        /// </summary>
        /// <param name="args"></param>
        public void MoveOutEventHandler(object args)
        {
            this.Image = null;
            //如果有子菜单，且子菜单项有被选中的，则本菜单项任然处于选中状态
            if (this.ChildMenuSprite != null && this.ChildMenuSprite.Items.Any(x => x.Status == MenuItemStatus.Select))
                this.Status = MenuItemStatus.Select;
            else
            {
                this.Status = MenuItemStatus.Normal;
                if (this.ChildMenuSprite != null)//不被选中的菜单项，其子菜单也需要被隐藏
                    this.ChildMenuSprite.SetShowing(false);
            }

        }
        /// <summary>
        /// 鼠标左键单击事件处理器
        /// </summary>
        /// <param name="args"></param>
        public void ClickLeftEventHandler(object args)
        {
            WorkType? type=null;
            switch (this.Text)
            {
                case "农 业": type = WorkType.Agriculture; break;
                case "商 业": type = WorkType.Commerce; break;
                case "筑 城": type = WorkType.Endurance; break;
                case "技 术": type = WorkType.Technology; break;
                case "文 化": type = WorkType.Culture; break;
                case "治 安": type = WorkType.PublicSecurity; break;
                case "民 心": type = WorkType.PopularFeelings; break;
                case "登 庸": type = WorkType.VisiteOfficer; break;
                case "任 命": 
                    ApointOfficialPositionListForm form = new ApointOfficialPositionListForm(MenuSprite.BuildingId);
                    form.ShowDialog();
                    break;
            }
            if (type == WorkType.Agriculture || type == WorkType.Commerce || type == WorkType.Technology ||
                type == WorkType.Culture ||
                type == WorkType.PublicSecurity || type == WorkType.PopularFeelings)
            {
                DispatchOfficerListForm form = new DispatchOfficerListForm(MenuSprite.BuildingId, (WorkType) type);
                form.ShowDialog();
            }
            else if(type==WorkType.VisiteOfficer)
            {
                Talents.VisiteOfficerListForm form = new Talents.VisiteOfficerListForm(MenuSprite.BuildingId, (WorkType)type);
                form.ShowDialog();
            }

        }
        protected override void LoadPic()
        {
            if (this.Image == null)
            {
                switch (Status)
                {
                    case MenuItemStatus.Normal:
                        this.Image = Game.Content.Load<Texture2D>(@"Images\views\building\menu\ContextMenuLeftClick");
                        break;
                    case MenuItemStatus.Select:
                        this.Image = Game.Content.Load<Texture2D>(@"Images\views\building\menu\ContextMenuLeftClickSelected");
                        break;
                    case MenuItemStatus.Disable:
                        this.Image = Game.Content.Load<Texture2D>(@"Images\views\building\menu\ContextMenuLeftDisabled");
                        break;
                }
            }

        }
    }
}
